﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Web;

namespace WebUploaderDemo.ashx
{
    /// <summary>
    /// webuploader 的摘要说明
    /// </summary>
    public class webuploader : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            string method = context.Request.QueryString["Method"].ToString();
            MethodInfo methodIndo = this.GetType().GetMethod(method);
            string result = methodIndo.Invoke(this, null).ToString(); //执行获取方法
            context.Response.ContentType = "text/plain";
            context.Response.Write(result);
        }

        public string Upload()
        {
            string result = string.Empty;
            if (HttpContext.Current.Request.Files.Count>0)
            {
                int fileCount = HttpContext.Current.Request.Files.Count;
                string fileName = string.Empty;
                string temp = string.Empty;
                for (int i = 0; i < fileCount; i++)
                {
                    HttpPostedFile file = HttpContext.Current.Request.Files[i];
                    if (file != null && file.ContentLength > 0)
                    {
                        // 获取上传的文件，保存到 应用程序目录
                        fileName = System.AppDomain.CurrentDomain.BaseDirectory + "upload/" + file.FileName;
                        file.SaveAs(fileName);
                        // 从服务器上读取文件
                        StreamReader reader = new StreamReader(fileName, Encoding.GetEncoding("GB2312"));
                        temp = reader.ReadLine();
                        reader.Close();
                    }
                }
            }
            
           
            return result;
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}